#define JMBCPLAYER_INSIDE
#include "jmbcPlayer.h"
#include <juanmabc.h>

// Context

#define STORE ID_SMALL_STORE

static jmbcPLAYER *store[STORE];

void
jmbcPlayerCreateContext(void)
{
	idClearStore(jmbcPLAYER, STORE, store);
}

// Generation / Deletion

static
void
Init(jmbcPLAYER *player)
{
	mlPointLoadZero(player->o);
	mlVectorLoadX(player->x);
	mlVectorLoadY(player->y);
	mlVectorLoadZ(player->z);
}

static
void
Term(jmbcPLAYER *player)
{
}

void
jmbcGenPlayers(unsigned int n, unsigned int *players)
{
	idGenObjects(jmbcPLAYER, STORE, store, Init, n, players);
}

void
jmbcDeletePlayers(unsigned int n, unsigned int *players)
{
	idDeleteObjects(store, Term, n, players);
}

// Operation

void
jmbcPlayerControl(unsigned int id, float controller[3])
{
	jmbcPLAYER *player;
	float t[3];

	player = store[id];

	mlVectorScaleTo(player->z, controller[1] * controller[2] * 2.0f, t); t[1] = 0.0f;
	mlVectorAdd(player->o, t);
	mlVectorRotate(player->x, mlVectorY, controller[0] * controller[2] * 3.0f);
	mlVectorRotate(player->y, mlVectorY, controller[0] * controller[2] * 3.0f);
	mlVectorRotate(player->z, mlVectorY, controller[0] * controller[2] * 3.0f);
}

void
jmbcPlayerMotion(unsigned int id, float x, float y)
{
	jmbcPLAYER *player;

	player = store[id];

	mlVectorRotate(player->x, mlVectorY, x);
	mlVectorRotate(player->y, mlVectorY, x);
	mlVectorRotate(player->y, player->x, y);
	mlVectorRotate(player->z, mlVectorY, x);
	mlVectorRotate(player->z, player->x, y);
}

void
jmbcPlayerLook(unsigned int id)
{
	jmbcPLAYER *player;
	float matrix[16];

	player = store[id];

	mlMatrixLoadLook(matrix, player->o, player->z, player->y);
	glLoadMatrixf(matrix);
}
